﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using YasharEl.Infrastructure.ApplicationModel.Bootstrap.Extensibility;
using YasharEl.Infrastructure.ApplicationModel.Bootstrap.Syntax;
using YasharEl.Infrastructure.ApplicationModel.Bootstrap.Reporting;

namespace YasharEl.Infrastructure.ApplicationModel.Bootstrap.Execution
{
    /// <summary>
    /// Synchronously executes the specified syntax on the provided extensions.
    /// </summary>
    /// <typeparam name="TExtension">The type of the extension</typeparam>
    public class SynchronousExecutor<TExtension> : IExecutor<TExtension>
        where TExtension : IBootstrappingExtension
    {
        public string Name
        {
            get
            {
                return this.GetType().FullNameToString();
            }
        }

        public void Execute(ISyntax<TExtension> syntax, IEnumerable<TExtension> extensions, IExecutionContext executionContext)
        {
            foreach (IExecutable<TExtension> executable in syntax)
            {
                IExecutableContext executableContext = executionContext.CreateExecutableContext(executable);
                executable.Execute(extensions, executableContext);
            }
        }

        public string Describe()
        {
            return "Runs all executables synchronously on the extensions in the order which they were added.";
        }
    }
}
